package com.hisense.store.batch.util;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Util
{

    public static boolean isFileNameValid(String fileName)
    {

        Pattern p = Pattern.compile("[0-9a-z_.]+");
        Matcher matcher = p.matcher(fileName);
        if (matcher.matches())
        {
            // 只允许16进制数据

            return true;
        }

        return false;
    }

    public static boolean isImageTypeValid(File o)
    {

        try
        {
            // Create an image input stream on the image
            ImageInputStream iis = ImageIO.createImageInputStream(o);

            // Find all image readers that recognize the image format
            Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
            if (!iter.hasNext())
            {
                // No readers found
                return false;
            }

            // Use the first reader
            ImageReader reader = iter.next();

            // Close stream
            iis.close();

            // Return the format name
            String name = reader.getFormatName();
            String[] imageFormats = { "jpg", "BMP", "bmp", "JPG", "jpeg", "png", "PNG", "JPEG", "GIF", "gif" };

            for (int i = 0; i < imageFormats.length; i++)
            {
                if (imageFormats[i].equalsIgnoreCase(name))
                {
                    return true;
                }
            }
            return false;
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean isEmpty(String tag)
    {

        if (tag == null || "".equals(tag))
            return true;

        return false;

    }

}
